import { createApp } from "vue";
import App from "./App.vue";
import "./registerServiceWorker";
import router from "./router/router.js";
import store from "./store";
import VueAxios from "vue-axios";
import axios from "./api/axios.js";
import "./assets/css/index.css";
import Vant from "vant";
import "vant/lib/index.css";
import waves from "./directives/waves";
import htmlPlus from "./utils/htmlPlus";
import "./assets/icon/iconfont.css";
// import Element from "element-plus";
// import "element-plus/lib/theme-chalk/index.css";
import "tracking";

// 重写 tracking 的视频捕获方法，因为不能停止 stop无效的bug
window.tracking.trackVideo_ = function (element, tracker) {
  console.log("trackVideo_");
  var canvas = document.createElement("canvas");
  var context = canvas.getContext("2d");
  var width;
  var height;

  var resizeCanvas_ = function () {
    width = element.offsetWidth;
    height = element.offsetHeight;
    canvas.width = width;
    canvas.height = height;
  };
  resizeCanvas_();
  element.addEventListener("resize", resizeCanvas_);

  var requestId;
  var stopped = false;
  var requestAnimationFrame_ = function () {
    requestId = window.requestAnimationFrame(function () {
      if (element.readyState === element.HAVE_ENOUGH_DATA) {
        try {
          // Firefox v~30.0 gets confused with the video readyState firing an
          // erroneous HAVE_ENOUGH_DATA just before HAVE_CURRENT_DATA state,
          // hence keep trying to read it until resolved.
          context.drawImage(element, 0, 0, width, height);
        } catch (err) {
          console.log(err);
        }
        window.tracking.trackCanvasInternal_(canvas, tracker);
      }
      if (stopped !== true) {
        requestAnimationFrame_();
      }
    });
  };
  var task = new window.tracking.TrackerTask(tracker);
  task.on("stop", function () {
    stopped = true;
    window.cancelAnimationFrame(requestId);
  });
  task.on("run", function () {
    stopped = false;
    requestAnimationFrame_();
  });
  return task.run();
};

window.htmlPlus = htmlPlus;
htmlPlus(() => {
  // 获取调用摄像头权限
  window.plus.camera.getCamera();
  var webview = window.plus.webview.currentWebview();
  var first = null;
  window.plus.key.addEventListener("backbutton", function () {
    webview.canBack(function (e) {
      if (e.canBack) {
        webview.back();
      } else {
        //webview.close(); //hide,quit
        //plus.runtime.quit();
        //首页返回键处理
        //处理逻辑：1秒内，连续两次按返回键，则退出应用；
        //首次按键，提示‘再按一次退出应用’
        if (!first) {
          first = new Date().getTime();
          // toast('双击返回键退出应用'); //调用自己写的吐丝提示 函数
          window.plus.nativeUI.toast("再按一次返回键退出应用", {
            duration: "short",
          }); //通过H5+ API调用Android 上的toast 提示框
          setTimeout(function () {
            first = null;
          }, 1000);
        } else {
          if (new Date().getTime() - first < 1400) {
            window.plus.runtime.quit();
          }
        }
      }
    });
  });
});

const app = createApp(App);

app.use(VueAxios, axios);

// app.use(Element);
app.use(store);
app.use(router);
app.use(Vant);

app.directive("waves", waves);

app.mount("#app");
